<?php
/************************************************************************************************
*   This file is a part of JV-LinkExchanger, a link exchange component for Joomla
*   Copyright (C) 2007-2008  Vijaykumar Niranjan 
*	
*   This program is free software; you can redistribute it and/or modify 
*   it under the terms of the GNU General Public License as published by 
*   the Free Software Foundation; either version 2 of the License, or 
*  (at your option) any later version. 
*
*   This program is distributed in the hope that it will be useful, 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
*   GNU General Public License for more details. 
*
*   You should have received a copy of the GNU General Public License 
*   along with this program; if not, write to the Free Software 
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

* 	JV-LinkExchanger - Link Exchange Component for Joomla 1.0.x
* 	@package JV-LinkExchanger
*   @SEF advance component extension
* 	@file version 1.2.5 RELEASE
* 	@author Vijaykumar Niranjan
* 	@link http://www.jv-extensions.com
* 	@copyright (C) 2007-2008 by Vijaykumar Niranjan
* 	@license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*  	This program is distributed with the hope that it will be useful,
*  	but WITHOUT ANY WARRANTY; without even the implied warranty of
*  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************************************/
class sef_jvlinx {

    function create ($string) {
        $sefstring = "";

		$task = sef_jvlinx::get_var($string, "task");		
		switch ($task)
		{
			case "addlink":	// add your link
			{
				$sefstring .= "addlink.html";
				break;
			}
			case "addbanner":	// add your link
			{
				$sefstring .= "addbanner.html";
				break;
			}
			case "checklinkstatus":
			{
				$sefstring .= "linkstatus.html";
				break;
			}
			case "recommend":
			{
				$sefstring .= "recommend.html";
				break;
			}		
			case "links":	// could be pagination for links/cat
			{
				$cid = sef_jvlinx::get_var($string, "cid");	
				$pg = sef_jvlinx::get_var($string, "pg");	

				if (($cid == 0) || ($cid == ""))
					$sefstring .= "links/page".$pg.".html";
				else
				{
					$cname = sef_jvlinx::f_refine(strtolower(stripslashes(sef_jvlinx::get_catname($cid))));	
					$sefstring .= $cname."/page".$pg.".html";
				}	
				break;						
			}	
			case "all_category_links":
			{
				$cid = sef_jvlinx::get_var($string, "cid");											
				$cname = sef_jvlinx::f_refine(strtolower(stripslashes(sef_jvlinx::get_catname($cid))));	
				
				$sby = sef_jvlinx::get_var($string, "sby");
				switch ($sby)
				{
					case 11: $suffix = "published.html"; break;
					case 12: $suffix = "ratings.html"; break;
					case 13: $suffix = "votes.html"; break;	
					default: $suffix = "index.html"; break;																	
				}

				$sefstring .= $cname."/".$suffix;
				break;						
			}																
			case "all_links":
			{
				$sby = sef_jvlinx::get_var($string, "sby");
				switch ($sby)
				{
					case 11: $suffix = "published.html"; break;
					case 12: $suffix = "ratings.html"; break;
					case 13: $suffix = "votes.html"; break;	
					default: $suffix = "index.html"; break;																	
				}

				$sefstring .= "links/".$suffix;
				break;						
			}																
			default:
			{
				if (sef_jvlinx::get_catformat() == "TYPE-3") // when main page loads
					$sefstring .= "links/page1.html";
				else				
					$sefstring .= "index.html";
				break;	
			}		
		}	
		return $sefstring;	
    }

    /**
    * Reverts to the Mambo query string out of the SEF advance URL
    * Input:
    *    $url_array, array, The SEF advance URL split in arrays (first custom virtual directory beginning at $pos+1)
    *    $pos, int, The position of the first virtual directory (component)
    * Output: $QUERY_STRING, string, Mambo query string (var1=$var1&var2=$var2)
    *    Note that this will be added to already defined first part (option=com_example&Itemid=$Itemid)
    **/
    function revert ($url_array, $pos) {
        // define all variables you pass as globals
        global $var1, $var2;
         
		 // Examine the SEF advance URL and extract the variables building the query string
        $QUERY_STRING = "";
		
		$var1 = sef_jvlinx::get_args($url_array, 1);
		$var2 = sef_jvlinx::get_args($url_array, 2);
		
		if ($var1 == "index.html")
		{
			$QUERY_STRING .= "";	// just show the home page
		}	
		else if ($var1 == "recommend.html")
		{
            $_GET['task'] = "recommend"; $_REQUEST['task'] = "recommend";			
			$QUERY_STRING .= "&task=recommend";		
		}	
		else if ($var1 == "linkstatus.html")
		{
            $_GET['task'] = "checklinkstatus"; $_REQUEST['task'] = "checklinkstatus";			
			$QUERY_STRING .= "&task=checklinkstatus";		
		}
		else if ($var1 == "addlink.html") // /addlink.html
		{
            $_GET['task'] = "addlink"; $_REQUEST['task'] = "addlink";			
			$QUERY_STRING .= "&task=addlink";		
		}		
		else if ($var1 == "addbanner.html") // /addlink.html
		{
            $_GET['task'] = "addbanner"; $_REQUEST['task'] = "addbanner";			
			$QUERY_STRING .= "&task=addbanner";		
		}		
		else // is a double variable url
		{
			if ($var1 == "links")	// links/{index.html/published.html/votes.html/ratings.html/page{n}.html}
			{
				if (eregi("page", $var2)) // /links/pagen.html
				{
					$pg = sef_jvlinx::get_pagenumber($var2);
					$_GET['task'] = "links"; $_REQUEST['task'] = "links";			
					$_GET['cid'] = 0; $_REQUEST['cid'] = 0;			
					$_GET['pg'] = $pg; $_REQUEST['pg'] = $pg;			
					$QUERY_STRING .= "&task=links&cid=0&pg=".$pg;
				}				
				else 
				{
					switch ($var2)
					{
						case "published.html": 
						{
							$_GET['task'] = "all_links"; $_REQUEST['task'] = "all_links";			
							$_GET['sby'] = 11; $_REQUEST['sby'] = 11;							
							$QUERY_STRING .= "&task=all_links&sby=11";
							break;
						}	
						case "ratings.html": 
						{
							$_GET['task'] = "all_links"; $_REQUEST['task'] = "all_links";			
							$_GET['sby'] = 12; $_REQUEST['sby'] = 12;							
							$QUERY_STRING .= "&task=all_links&sby=12";
							break;
						}	
						case "votes.html": 
						{
							$_GET['task'] = "all_links"; $_REQUEST['task'] = "all_links";			
							$_GET['sby'] = 13; $_REQUEST['sby'] = 13;							
							$QUERY_STRING .= "&task=all_links&sby=13";
							break;
						}	
						default: // index.html
						{
							$_GET['task'] = "all_links"; $_REQUEST['task'] = "all_links";			
							$_GET['sby'] = 3; $_REQUEST['sby'] = 3;							
							$QUERY_STRING .= "&task=all_links&sby=3";
							break;
						}
					}				
				}						
			}
			else	// <catname>/{index.html/published.html/votes.html/ratings.html/page{n}.html}
			{
				$cid = sef_jvlinx::get_catid($var1);
				if (eregi("page", $var2)) // /<catname>/pagen.html
				{
					$pg = sef_jvlinx::get_pagenumber($var2);
					$_GET['task'] = "links"; $_REQUEST['task'] = "links";			
					$_GET['cid'] = $cid; $_REQUEST['cid'] = $cid;			
					$_GET['pg'] = $pg; $_REQUEST['pg'] = $pg;			
					$QUERY_STRING .= "&task=links&cid=".$cid."&pg=".$pg;
				}										
				else
				{
					switch ($var2)
					{
						case "published.html": 
						{
							$_GET['task'] = "all_category_links"; $_REQUEST['task'] = "all_category_links";			
							$_GET['cid'] = $cid; $_REQUEST['cid'] = $cid;								
							$_GET['sby'] = 11; $_REQUEST['sby'] = 11;							
							$QUERY_STRING .= "&task=all_category_links&cid=".$cid."&sby=11";
							break;
						}	
						case "ratings.html": 
						{
							$_GET['task'] = "all_category_links"; $_REQUEST['task'] = "all_category_links";			
							$_GET['cid'] = $cid; $_REQUEST['cid'] = $cid;								
							$_GET['sby'] = 12; $_REQUEST['sby'] = 12;							
							$QUERY_STRING .= "&task=all_category_links&cid=".$cid."&sby=12";
							break;
						}	
						case "votes.html": 
						{
							$_GET['task'] = "all_category_links"; $_REQUEST['task'] = "all_category_links";			
							$_GET['cid'] = $cid; $_REQUEST['cid'] = $cid;								
							$_GET['sby'] = 13; $_REQUEST['sby'] = 13;							
							$QUERY_STRING .= "&task=all_category_links&cid=".$cid."&sby=13";
							break;
						}	
						default: // index.html
						{
							$_GET['task'] = "all_category_links"; $_REQUEST['task'] = "all_category_links";			
							$_GET['cid'] = $cid; $_REQUEST['cid'] = $cid;								
							$_GET['sby'] = 3; $_REQUEST['sby'] = 3;							
							$QUERY_STRING .= "&task=all_category_links&cid=".$cid."&sby=3";
							break;
						}
					}
				}	
			}
		}
    }
	
	function get_var($url, $param)
	{
		if (eregi("&amp;$param=",$url)) 
		{
        	$temp = split("&amp;$param=", $url);
            $temp = split("&", $temp[1]);
			$val = $temp[0];
			return $val;
        }
		return "";
	}

	function get_catname($id)
	{
		global $database;
		
		$database->setQuery("select name from #__jvlinx_categories where id = '".$id."'");
		$database->query();
		$mr = NULL;
		$database->loadObject($mr);
		return $mr->name;
	}

	function get_catformat()
	{
		global $database;
		
		$database->setQuery("select fe_catformat from #__jvlinx_settings");
		$database->query();
		$mr = NULL;
		$database->loadObject($mr);
		return $mr->fe_catformat;
	}	
	
	function f_refine($string)
	{
		$string = str_replace(" & ", "_and_", $string);
		return str_replace(" ", "_", $string);
	}
	
	function get_args($in_arr, $num)
	{
		$incr = $num + 1;
		
        if (isset($in_arr[$pos+$incr]) && $in_arr[$pos+$incr]!="") 
			return $in_arr[$pos+$incr];
		
		return "";	
	}

	function get_catid($name)	// input can have _and_/_ in it too.
	{
		global $database;
		
		$name = str_replace("_and_", " & ", $name);
		$name = str_replace("_", " ", $name);
		
		$database->setQuery("select id from #__jvlinx_categories where lower(name) = '".$name."'");
		$database->query();
		$mr = NULL;
		$database->loadObject($mr);
		return $mr->id;
	}
	
	function get_pagenumber($str)
	{
		$e = strpos($str, ".");
		$pg = substr($str, 4, ($e-4));
		return $pg;
	}
}
?>